import os
import sys
import logging
from acc_active_label import get_acc_scid_time
from pilot_active_label import get_pilot_scid_time
from nnp_active_label import get_nnp_scid_time
from aeb_active_label import get_aeb_scid_time
from fcw_level2_label import get_fcw_level2_scid_time
from functional_degradation_label import  get_functional_degradation_scid_time
from function_exit_nnp_label import get_function_exit_scid_time
from expected_takeover_ads_label import get_expected_takeover_ads_scid_time
from function_exit_adas_label import get_function_exit_adas_scid_time
from suspected_crash import  get_suspected_crash_scid_time
from emergency_steering_label import get_emergency_steering_scid_time
from no_expect_take_over import get_no_expect_take_over_scid_time
import pickle

# 配置日志记录
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')


def get_bagid(object_key):
    # 使用split函数将字符串分割成部分
    parts = object_key.split('-')
    # 提取最后一部分
    last_part = parts[-1]
    # 可以选择去掉文件扩展名（.zip）
    bagid = last_part.split('.')[0]
    return bagid

def get_all_path(pk_list):
    path_st_100ms=""
    path_st_can_out=""
    path_st_can_in=""
    path_st_20ms=""
    for path in pk_list:
        if "st_100ms.pkl" in path:
            path_st_100ms=path
        elif "st_can_out.pkl" in path:
            path_st_can_out=path
        elif "st_can_in.pkl" in path:
            path_st_can_in=path
        elif "st_20ms.pkl" in path:
            path_st_20ms=path
    return path_st_100ms,path_st_can_out,path_st_can_in,path_st_20ms

def get_st_can_out_path(pk_list):
    path_st_100ms=""
    for path in pk_list:
        if "st_can_out.pkl" in path:
            path_st_100ms=path
    return path_st_100ms

def get_st_100ms_path(pk_list):
    path_st_100ms=""
    for path in pk_list:
        if "st_100ms.pkl" in path:
            path_st_100ms=path
    return path_st_100ms
def get_st_20ms_path(pk_list):
    path_st_20ms=""
    for path in pk_list:
        if "st_20ms.pkl" in path:
            path_st_20ms=path
    return path_st_20ms
#列举文件：
def list_files_in_directory(directory):
    file_list = []
    for root, dirs, files in os.walk(directory):
        for filename in files:
            if ".pkl" in filename:
                filepath = os.path.join(root, filename)
                file_list.append(filepath)
    return file_list

if __name__ == '__main__':
    bucket_name="advc-data-upload-2100095994-cn-beijing"
    # object_key="EP40/TDA4/SC/v2212963022/20231227/17/fcm5uk2bc77u0nv1lv4og-EP40_TDA4_drive_sc-20231227-171028-2302.zip"
    # object_key = os.environ.get('BAG_PATH')
    object_key=sys.argv[1]
    bagid=get_bagid(object_key)
    arr=object_key.split("/")
    vechicle_id=arr[3]
    daystr = arr[4]
    hourstr = arr[5]
    filename=arr[6]
    filearr=filename.split("-")
    file_prefix=filearr[0]
    savepath = "/data/code/input/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/source_json/"
    pk_path = "/data/code/input/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/source_json/"
    # /data/code/input/v2305175911/20231225/19/fcm4mhvrc77u0nv1hhbqg/source_json/3307/hz_st_upload/pk
    pk_list=list_files_in_directory(pk_path)
    # print("pk_list: "+str(pk_list))
    if len(pk_list)>0:
        if bagid == "2300":
            pk_file_100ms = get_st_100ms_path(pk_list)
            logging.info("pk_file: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            pk_file_20ms = get_st_20ms_path(pk_list)
            get_acc_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)
            # 模型部分

        if bagid == "2301":
            pk_file = get_st_100ms_path(pk_list)
            logging.info("pk_file: " + pk_file)
            pk_name = pk_file.split("/pk/")[1]
            vechicle_id = pk_file.split("/")[4]
            day = pk_file.split("/")[5]
            hour = pk_file.split("/")[6]
            uuids = pk_file.split("/")[7]
            bagid = pk_file.split("/")[9]
            file_type = pk_file.split("/")[10]
            logging.info("bagid: " + bagid)
            get_pilot_scid_time(pk_file, vechicle_id, bagid, daystr, hourstr)

        if bagid == "2302":
            path_st_100ms, path_st_can_out, path_st_can_in, path_st_20ms = get_all_path(pk_list)
            logging.info("pk_file: " + path_st_100ms)
            pk_name = path_st_100ms.split("/pk/")[1]
            vechicle_id = path_st_100ms.split("/")[4]
            day = path_st_100ms.split("/")[5]
            hour = path_st_100ms.split("/")[6]
            uuids = path_st_100ms.split("/")[7]
            bagid = path_st_100ms.split("/")[9]
            file_type = path_st_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            get_nnp_scid_time(path_st_can_out, vechicle_id, bagid, daystr, hourstr)

        # 已校验
        if bagid == "2303":
            pk_file_100ms = get_st_100ms_path(pk_list)
            pk_file_20ms = get_st_20ms_path(pk_list)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            get_expected_takeover_ads_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)

        # 已验证
        if bagid == "2304":
            pk_file_out = get_st_can_out_path(pk_list)
            pk_file_100ms = get_st_100ms_path(pk_list)
            pk_file_20ms = get_st_20ms_path(pk_list)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            get_function_exit_scid_time(pk_file_out, vechicle_id, bagid, daystr, hourstr)

        if bagid == "2305":
            pk_file_out = get_st_can_out_path(pk_list)
            pk_file_100ms = get_st_100ms_path(pk_list)
            pk_file_20ms = get_st_20ms_path(pk_list)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            get_emergency_steering_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)

        if bagid == "2309":
            pk_file_20ms = get_st_20ms_path(pk_list)
            pk_file = get_st_100ms_path(pk_list)
            logging.info("pk_file: " + pk_file)
            pk_name = pk_file.split("/pk/")[1]
            vechicle_id = pk_file.split("/")[4]
            day = pk_file.split("/")[5]
            hour = pk_file.split("/")[6]
            uuids = pk_file.split("/")[7]
            bagid = pk_file.split("/")[9]
            file_type = pk_file.split("/")[10]
            logging.info("bagid: " + bagid)
            get_fcw_level2_scid_time(pk_file, vechicle_id, bagid, daystr, hourstr)

        if bagid == "2310":
            pk_100ms_file = get_st_100ms_path(pk_list)
            logging.info("pk_file: " + pk_100ms_file)
            pk_name = pk_100ms_file.split("/pk/")[1]
            vechicle_id = pk_100ms_file.split("/")[4]
            day = pk_100ms_file.split("/")[5]
            hour = pk_100ms_file.split("/")[6]
            uuids = pk_100ms_file.split("/")[7]
            bagid = pk_100ms_file.split("/")[9]
            file_type = pk_100ms_file.split("/")[10]
            logging.info("bagid: " + bagid)
            get_no_expect_take_over_scid_time(pk_100ms_file, vechicle_id, daystr, hourstr, bagid, uuids, file_type)

        # 已验证
        if bagid == "2311":
            pk_file_out = get_st_can_out_path(pk_list)
            pk_file_100ms = get_st_100ms_path(pk_list)
            logging.info("pk_file_out: " + pk_file_out)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_out.split("/pk/")[1]
            vechicle_id = pk_file_out.split("/")[4]
            day = pk_file_out.split("/")[5]
            hour = pk_file_out.split("/")[6]
            uuids = pk_file_out.split("/")[7]
            bagid = pk_file_out.split("/")[9]
            file_type = pk_file_out.split("/")[10]
            logging.info("bagid: " + bagid)
            get_functional_degradation_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)

        # 已校验
        if bagid == "2314":
            pk_file_100ms = get_st_100ms_path(pk_list)
            pk_file_20ms = get_st_20ms_path(pk_list)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            logging.info("bagid: " + bagid)
            get_function_exit_adas_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)

        if bagid == "3307":
            pk_file_20ms = get_st_20ms_path(pk_list)
            pk_file_100ms = get_st_100ms_path(pk_list)
            logging.info("pk_file_100ms: " + pk_file_100ms)
            pk_name = pk_file_100ms.split("/pk/")[1]
            vechicle_id = pk_file_100ms.split("/")[4]
            day = pk_file_100ms.split("/")[5]
            hour = pk_file_100ms.split("/")[6]
            uuids = pk_file_100ms.split("/")[7]
            bagid = pk_file_100ms.split("/")[9]
            file_type = pk_file_100ms.split("/")[10]
            get_suspected_crash_scid_time(pk_file_100ms, vechicle_id, bagid, daystr, hourstr)

        if bagid == "3308":
            pk_file = get_st_100ms_path(pk_list)
            logging.info("pk_file: " + pk_file)
            pk_name = pk_file.split("/pk/")[1]
            vechicle_id = pk_file.split("/")[4]
            day = pk_file.split("/")[5]
            hour = pk_file.split("/")[6]
            uuids = pk_file.split("/")[7]
            bagid = pk_file.split("/")[9]
            file_type = pk_file.split("/")[10]
            logging.info("bagid: " + bagid)
            get_aeb_scid_time(pk_file, vechicle_id, bagid, daystr, hourstr)




